// message box module

/// <reference path="../typings/jquery/jquery.d.ts" />
/// <reference path="../typings/jquery/jqueryui.d.ts" />

// import localization module
import Loc = require("./Localization");

// confirming result
export enum eConfirmResult { Yes, No };

export interface IConfirmHandler {
    (result: eConfirmResult): void;
}

export class MessageBox {

    // singleton
    private static mInstance: MessageBox;

    // localization class
    private mLocalization: Loc.Localization;

    // constructor
    public constructor() {
        this.mLocalization = Loc.Localization.Instance;
    }


    // singleton instance property getter
    public static get Instance() {
        if (this.mInstance == null)
            this.mInstance = new MessageBox();
        return this.mInstance;
    }

    // asks for confirmation
    public Confirm(message: string, title: string, handler: IConfirmHandler, dlgWidth: number = 500) {
        // localize messages
        message = this.mLocalization.Get(message);
        title = this.mLocalization.Get(title);

        var btnYes = this.mLocalization.Get("btnYes");
        var btnNo = this.mLocalization.Get("btnNo");

        // show the message box
        $("#idMessage").html(message);
        $("#idMessageBox").attr("title", title);
        $("#idMessageBox").dialog({
            modal: true,
            width: dlgWidth,
            draggable: false,
            resizable: false,
            buttons: [
                {
                    id: "idYes",
                    text: btnYes,
                    click: function () {
                        $(this).dialog("close");
                        handler(eConfirmResult.Yes);
                    }
                },
                {
                    id: "idNo",
                    text: btnNo,
                    click: function () {
                        $(this).dialog("close");
                        handler(eConfirmResult.No);
                    }
                }
            ]
        });
    }

    // shows the message box
    public ShowModal(message: string, title: string = "", localizeMsg: boolean = true, dlgWidth: number = 500, dlgHeight: number = 300): void {
        // localize messages
        if (localizeMsg) {
            message = this.mLocalization.Get(message);
        }

        if (title.length > 0) {
            title = this.mLocalization.Get(title);
        }

        var btnOK = this.mLocalization.Get("btnOK");

        // show the message box
        $("#idMessage").html(message);
        $("#idMessageBox").attr("title", title);
        $("#idMessageBox").dialog({
            modal: true,
            width: dlgWidth,
            draggable: false,
            resizable: false,
            buttons: [{
                id: "idOK",
                text: btnOK,
                click: function () {
                    $(this).dialog("close");
                }
            }]
        });
    }
}
